#include "stdafx.h"
#include "TouchPad.h"
#include "../Game.h"

int CTouchPad::m_nLastX = 0;
int CTouchPad::m_nLastY = 0;

CTouchPad::CTouchPad():m_nTouchEventCount(0)
{

}

CTouchPad::~CTouchPad()
{

}

//input methoed
void CTouchPad::PadTouchEvent(int x, int y, void *touchPointer, int cType)
{
	//if (m_nTouchEventCount >= MAX_SIMULTANEOUS_TOUCHES)
	//	return;

	int index = 0;
	for(int i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++)
	{
		if(m_TouchEvents[i].benable == false)
		{
			index = i;
			break;
		}
	}

	m_TouchEvents[index] = CTouchEvent(x, y, touchPointer, TOUCH_DOWN, cType, false, true);
	m_nTouchEventCount++;
	m_nLastX = x;
	m_nLastY = y;
}

void CTouchPad::PadUntouchEvent(int x, int y, void *touchPointer, int cType)
{
	//if (m_nTouchEventCount >= MAX_SIMULTANEOUS_TOUCHES)
	//	return;

	int index = 0;
	for(int i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++)
	{
		if(m_TouchEvents[i].benable == false)
		{
			index = i;
			break;
		}
	}
	
	m_TouchEvents[index] = CTouchEvent(x, y, touchPointer, TOUCH_UP, cType, false, true);
	m_nTouchEventCount++;
	m_nLastX = x;
	m_nLastY = y;
}

void CTouchPad::PadMoveEvent(int x, int y, void *touchPointer)
{
	//if (m_nTouchEventCount >= MAX_SIMULTANEOUS_TOUCHES)
	//	return;

	int index = 0;
	for(int i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++)
	{
		if(m_TouchEvents[i].benable == false)
		{
			index = i;
			break;
		}
	}
	
	m_TouchEvents[index] = CTouchEvent(x, y, touchPointer, TOUCH_MOVE, CT_NONE, false, true);
	m_nTouchEventCount++;
	m_nLastX = x;
	m_nLastY = y;
}

void CTouchPad::PadCancelEvent(int x, int y, void *touchPointer)
{
	//if (m_nTouchEventCount >= MAX_SIMULTANEOUS_TOUCHES)
	//	return;

	int index = 0;
	for(int i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++)
	{
		if(m_TouchEvents[i].benable == false)
		{
			index = i;
			break;
		}
	}
	
	m_TouchEvents[index] = CTouchEvent(x, y, touchPointer, TOUCH_CANCEL, CT_NONE, false, true);
	m_nTouchEventCount++;
	m_nLastX = x;
	m_nLastY = y;
}


//////////////////////////////////////////////////////////
//click
CTouchEvent* CTouchPad::CheckClickedInRegion(rect<int> * el)
{
	for(int i = MAX_SIMULTANEOUS_TOUCHES - 1; i>=0; i--)
	{
		if(m_TouchEvents[i].benable)
		{
			if(m_TouchEvents[i].type == TOUCH_DOWN)
			{
				if(el->isPointInside(position2d<int>(m_TouchEvents[i].x, m_TouchEvents[i].y)))
				{
					return &m_TouchEvents[i];
				}
			}
		}
	}
	
	return NULL;
}

CTouchEvent* CTouchPad::CheckClicked()
{
	for(int i=MAX_SIMULTANEOUS_TOUCHES-1; i>=0; i--)
	{
		if(m_TouchEvents[i].benable)
		{
			if(m_TouchEvents[i].type == TOUCH_DOWN)
			{
				return &m_TouchEvents[i];
			}
		}
	}

	return NULL;
}

//unclick
CTouchEvent* CTouchPad::CheckUnClicked(void* touchPointer)
{
	for(int i=MAX_SIMULTANEOUS_TOUCHES-1; i>=0; i--)
	{
		if(m_TouchEvents[i].benable)
		{
			if((m_TouchEvents[i].type == TOUCH_UP || m_TouchEvents[i].type == TOUCH_CANCEL) && (touchPointer == NULL || m_TouchEvents[i].touchPointer == touchPointer))
			{
				return &m_TouchEvents[i];
			}
		}
	}

	return NULL;
}

CTouchEvent* CTouchPad::CheckUnClickedInRegion(rect<int> * el, void* touchPointer)
{
	for(int i=MAX_SIMULTANEOUS_TOUCHES-1; i>=0; i--)
	{
		if(m_TouchEvents[i].benable)
		{
			if((m_TouchEvents[i].type == TOUCH_UP || m_TouchEvents[i].type == TOUCH_CANCEL) && (touchPointer == NULL || m_TouchEvents[i].touchPointer == touchPointer))
			{
				if(el->isPointInside(position2d<int>(m_TouchEvents[i].x, m_TouchEvents[i].y)))
				{
					return &m_TouchEvents[i];
				}
			}
		}
	}

	return NULL;
}

//move

CTouchEvent* CTouchPad::CheckMovedInRegion(rect<int> * el, void* touchPointer)
{
	for(int i = MAX_SIMULTANEOUS_TOUCHES-1; i>=0; i--)
	{
		if(m_TouchEvents[i].benable)
		{
			if(m_TouchEvents[i].type == TOUCH_MOVE && (touchPointer == NULL || m_TouchEvents[i].touchPointer == touchPointer))
			{
				if(el->isPointInside(position2d<int>(m_TouchEvents[i].x, m_TouchEvents[i].y)))
				{
					return &m_TouchEvents[i];
				}
			}
		}
	}
	return NULL;
}

CTouchEvent* CTouchPad::CheckMoved(void* touchPointer)
{
	for(int i=MAX_SIMULTANEOUS_TOUCHES-1; i>=0; i--)
	{
		if(m_TouchEvents[i].benable)
		{
			if(m_TouchEvents[i].type == TOUCH_MOVE && (touchPointer == NULL || m_TouchEvents[i].touchPointer == touchPointer))
			{			
				return &m_TouchEvents[i];
			}
		}
	}
	return NULL;
}


//unmoved
CTouchEvent* CTouchPad::CheckUnMovedInRegion(rect<int> * el)
{
	for(int i=MAX_SIMULTANEOUS_TOUCHES-1; i>=0; i--)
	{
		if(m_TouchEvents[i].benable)
		{
			if(m_TouchEvents[i].type == TOUCH_CANCEL)
			{
				if(el->isPointInside(position2d<int>(m_TouchEvents[i].x, m_TouchEvents[i].y)))
				{
					return &m_TouchEvents[i];
				}
			}
		}
	}

	return NULL;
}


void CTouchPad::Clear()
{
	m_nTouchEventCount = 0;
}

void CTouchPad::Update()
{
	for(int i=MAX_SIMULTANEOUS_TOUCHES-1; i>=0; i--)
	{
		if(m_TouchEvents[i].benable)
		{
			if(!m_TouchEvents[i].bchecked)
			{
				m_TouchEvents[i].bchecked = true;
			}
			else
			{
				m_TouchEvents[i].benable = false;
				m_nTouchEventCount--;
			}
		}
	}
}


